package com.example.leetcode.high;

import com.sun.javafx.binding.StringFormatter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution4 {

    public static void main(String[] args) {
        int[][] board = new int[][]{
                {0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0},
                {0, 0, 1, 1, 1, 0},
                {0, 1, 1, 1, 0, 0},
                {0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0}
        };
        new Solution4().gameOfLife(board);
    }

    public void gameOfLife(int[][] board) {
        int[][] output = new int[board.length][];
        for (int x = 0; x < board.length; x++) {
            int[] board2 = new int[board[x].length];
            for (int y = 0; y < board[x].length; y++) {
                int status = getStatus(board, x, y);
//                System.out.println("返回的状态：" + status);
                board2[y] = status;
            }
            output[x] = board2;
        }
        for (int x = 0; x < output.length; x++) {
            for (int y = 0; y < output[x].length; y++) {
                System.out.print(output[x][y] + "\t");
            }
            System.out.println();
        }
        for (int x = 0; x < output.length; x++) {
            int[] board2 = new int[output[x].length];
            for (int y = 0; y < output[x].length; y++) {
                board2[y] = output[x][y];
            }
            board[x] = board2;
        }
    }

    public int getStatus(int[][] board, int x, int y) {
        Map<String, Integer> statusMap = new HashMap<>();
        statusMap.put("alive",0);
        int posStatus = board[x][y];
        check(board, x - 1, y, statusMap);
        check(board, x - 1, y - 1, statusMap);
        check(board, x - 1, y + 1, statusMap);
        check(board, x + 1, y, statusMap);
        check(board, x + 1, y - 1, statusMap);
        check(board, x + 1, y + 1, statusMap);
        check(board, x, y - 1, statusMap);
        check(board, x, y + 1, statusMap);
        int alive = statusMap.get("alive");
        System.out.println(StringFormatter.format("当前状态：x:%s,y:%s,状态%s", x, y, posStatus).getValue());
        //如果活细胞周围八个位置的活细胞数少于两个，则该位置活细胞死亡；
        if (posStatus == 1 && alive < 2) {
            return 0;
        }
        //如果活细胞周围八个位置有两个或三个活细胞，则该位置活细胞仍然存活；
        if (posStatus == 1 && (alive == 2 || alive == 3)) {
            return 1;
        }
        //如果活细胞周围八个位置有超过三个活细胞，则该位置活细胞死亡；
        if (posStatus == 1 && alive > 3) {
            return 0;
        }
        //如果死细胞周围正好有三个活细胞，则该位置死细胞复活；
        if (posStatus == 0 && alive == 3) {
            return 1;
        }
        System.out.println();
        return posStatus;
    }

    public void check(int[][] board, int posX, int posY, Map<String, Integer> statusMap) {
        if (posX < 0 || posX >= board.length) {
            return;
        }
        if (posY < 0 || posY >= board[posX].length) {
            return;
        }
        int check = board[posX][posY];
        if (check == 1){
            Integer alive = statusMap.get("alive");
            statusMap.put("alive", ++alive);
        }
    }
}
